package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.pojo.Account;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.LoginService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class
LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account account1 = loginService.login(account);
        //登录成功，生成令牌，下发令牌
        if (account1 != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", account1.getId());
            claims.put("name", account1.getName());
            claims.put("username", account1.getUsername());
            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.error("用户名错误或密码错误");
    }

}